home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 43
/
Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso
/
-serious-
/
comms
/
other
/
getit
/
core
/
getitfun.rexx
< prev
Wrap
OS/2 REXX Batch file
|
1999-06-14
|
11KB
|
324 lines
/* */
signal on break_c
signal on halt
signal on syntax
ADDRESS VALUE word(arg(1),1)
"MACRO" x2d(pragma(ID))
call readArguments
call getStrings
call resolveHost
call connectHost
call sendRequest
call receiveHead
call openFile
call SetSocketSignals(2**12)
call parseLength
call receive
exit
/**************************************************************************/
receive: procedure expose global.
dummyL=200
dummyS=copies(" ",dummyL)
if global.range~="" then tl=global.range
else tl=0
ctl=0
"CURRENT" tl
cps=0
go=1
sel.read.0=global.sock
call time("R")
stal=0
do while go
string=""
res = WaitSelect("SEL",0,800000,2**12)
et=time("E")
if and(sel.signals,2**12)~=0 then call break_c
ets="ETime:" etime(et)
if res=0 then do
stal=stal+1
if stal>=5 then string=ets global.str.STALLED tl global.str.BYTES
end
else do
stal=0
len=recv(global.sock,"BUF",1024)
if len<0 then call err global.str.RXERR
if len>0 then do
if WriteCH("OUT",buf)~=length(buf) then call err global.str.TXERR
tl=tl+len
ctl=ctl+len
end
else go=0
end
if stal<5 then do
if ctl~=0 & et~=0 then cps=(ctl/et)%1
if global.length~="" then do
"CURRENT" tl
string=ets global.str.SIZE":" tl || "/"global.length "(" || (tl*100)%global.length"%%)"
end
else string=ets "-" tl "("global.str.UNKNOWSIZE")"
string=string global.str.AT cps "cps"
end
if string~="" then do
string=left(string||dummyS,dummyL)
"INFO" string
end
end
et=time("E")
call CloseSocket(global.sock)
if global.length~="" then
if tl~=global.length then call err global.str.ASPECTED global.length "-" global.str.RECEIVED tl "bytes"
"END"
return
/**************************************************************************/
readArguments: procedure expose global.
parm.5.value=80
parm.6.value=0
parm.7.value=""
parm.8.value=8080
parm.10.value=""
parm.11.value=""
parm.12.value=""
parm.13.value=""
parm.14.value=""
/* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 */
TEMPLATE = "PORT/A,URL/A,HOST/A,REMOTE/A,LOCALE/A,HOSTPORT/K/N,TIMEOUT/K/N,PROXY/K,PROXYPORT/K/N,NOCACHE/S,LOGIN/K,PASS/K,RANGE/K/N,IMS/K,REFERER/K"
if ~RMH_ReadArgs(TEMPLATE) then call ERR DosString(IoErr())
global.port=parm.0.value
global.url=parm.1.value
global.host=parm.2.value
global.remote=parm.3.value
global.locale=parm.4.value
global.hostport=parm.5.value
global.timeout=parm.6.value
global.proxy=parm.7.value
global.proxyport=parm.8.value
global.nocache=parm.9.flag
global.login=parm.10.value
global.pass=parm.11.value
global.range=parm.12.value
global.ims=parm.13.value
global.referer=parm.14.value
global.cat="GetIt.catalog"
global.u="http://"
if global.login~="" then global.u=global.u||global.login":"global.pass"@"
global.u=global.u||global.host":"global.hostport"/"global.remote global.referer
return
/**************************************************************************/
info: procedure expose global.
parse arg msg
"INFO" msg
return
/**************************************************************************/
err: procedure expose global. sigl rc
parse arg s,res
"CURRENT" 0
"TOTAL" 1
"ERR" s
exit
/**************************************************************************/
error:
call err "Error" sigl rc
exit
syntax:
call err "Syntax" sigl rc
exit
halt:
exit
break_c:
call err global.str.INTERRUPTED
/**************************************************************************/
encodeB64: procedure
parse arg s
s=c2b(s)
a=""
do while s~=""
parse var s c +6 s
a=a||substr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",c2d(b2c(left(c"0000",6)))+1,1)
end
l=length(c)
if l<6 then a=a||copies("=",(6-l)/2)
return a
/**************************************************************************/
etime: procedure
parse arg s
s=s%1
m=s%60
s=s//60
h=m%60
m=m//60
return right("00"h,2)":"right("00"m,2)":"right("00"s,2)
/**************************************************************************/
resolveHost: procedure expose global.
if global.proxy~="" then do
call info "Resolving host <"global.proxy">..."
global.sin.addrAddr=resolve(global.proxy)
if global.sin.addrAddr==-1 then call err "Host <"global.proxy">" global.str.HOSTNTFOUND".",1
global.sin.addrPort=global.proxyport
global.peer="<"global.proxy":"global.proxyport">"
end
else do
call info global.str.RESOLVINGHOST "<"global.host">..."
global.sin.addrAddr=resolve(global.host)
if global.sin.addrAddr==-1 then call err "Host <"global.host">" global.str.HOSTNTFOUND".",1
global.sin.addrPort=global.hostport
global.peer="<"global.host":"global.hostport">"
end
return
/**************************************************************************/
connectHost: procedure expose global.
global.sin.addrFamily="INET"
global.sock=socket("INET","STREAM")
if global.sock<0 then call err global.str.NOSOCKET
call info global.str.CONNECTING
if global.timeout>0 then do
global.tim=CreateTimer()
ts=TimerSignal(global.tim)
call SetSocketSignals(or(ts,2**12))
call StartTimer(global.tim,global.timeout)
end
if connect(global.sock,"GLOBAL.SIN")<0 then call err global.str.CANTCONNECT global.peer
return
/**************************************************************************/
sendrequest: procedure expose global.
call info global.str.SENDINGREQ
if global.range="" then httpV = "HTTP/1.1"
else httpV = "HTTP/1.0"
if left(global.remote,1)~="/" then global.remote="/"global.remote
if global.proxy~="" then req="GET" global.url httpV
else req="GET" global.remote httpV
hst="Host:" global.host
usr="User-Agent: GetIt/4.0 (Amiga-ARexx-rxsocket.library)"
if global.login~="" then auth="Authorization: Basic" encodeB64(global.login":"global.pass)
else auth=""
if global.nocache then cache="Pragma: no-cache"
else cache=""
req=req"D0A"x || usr"D0A"x
if auth~="" then req=req||auth"D0A"x
if cache~="" then req=req||cache"D0A"x
if hst~="" then req=req||hst"D0A"x
if global.range~="" then req=req||"Range: bytes="global.range"-" || "D0A"x
if global.ims~="" then req=req||"If-modified-since:" global.ims || "D0A"x
if global.referer~="" then req=req||"Referer:" global.referer || "D0A"x
req=req"D0A"x
if send(global.sock,req)~=length(req) then call err global.str.TXERR
return
/**************************************************************************/
receiveHead: procedure expose global.
call info global.str.HEADREC
len=recvline(global.sock,"BUF",256)
if len<0 then call err global.str.RXTIMEOUTERR
if len=0 then call err global.str.EMPTY
if left(buf,5)~="HTTP/" then call err global.str.BADANSWER
parse var buf "HTTP/"ver code coderest"D0A"x
global.rhost=""
go=1
global.length=""
last=""
mime=""
hs="HTTP/"ver code coderest"A"x
do while go
len=RecvLine(global.sock,"BUF",256)
if len<0 then call err global.str.RXTIMEOUTERR
parse var buf b": "rest"D0A"x
select
when b=="Content-Length" then global.length=rest
when b=="Last-Modified" then last=rest
when b=="Content-type" then mime=rest
when b=="WWW-Authenticate" then global.reqauth=rest
when b=="Location" then global.location=rest
when b=="Host" then global.rhost=rest
otherwise nop
end
go=(buf~="D0A"x)
if go then do
hs=hs || b":" rest || "A"x
end
end
if global.timeout>0 then call FreeTimer(global.tim)
if code=401 then do
"INFO" code coderest
"AUTH" global.reqauth
exit
end
if code=301 | code=302 then do
"MOVED" global.location global.rhost
exit
end
if code=304 then call err global.str.NOTMODIFIED global.ims
if global.range~="" then do
if code=200 then do
"ASKOVER"
if rc then do
global.range=""
end
else call err global.str.EXITED
end
else if code~=206 then call err global.str.SERVERERR":" code coderest
end
else if code~=200 then call err global.str.SERVERERR":" code coderest
return
/**************************************************************************/
openFile: procedure expose global.
if global.range="" then res=Open("OUT",global.locale,"W")
else res=Open("OUT",global.locale,"A")
if ~res then call err "Can't open file",1
call SetComment(global.locale,global.u)
fs=statef(global.locale)
if fs~="" then parse var fs d global.size
return
/**************************************************************************/
parseLength: procedure expose global.
tempLen=global.length
if global.length~="" then do
if global.range~="" then do
global.length=global.length+global.range
if global.range>=global.size then call err "Range:"global.range" >= Length:"global.size
end
global.l=global.l || global.length
end
else global.l=global.l || global.str.UNKONW
if global.length~="" then "TOTAL" global.length
return
/**************************************************************************/
getStrings: procedure expose global.
catalog=OpenCatalog(global.cat,"english",0)
global.str.STALLED=GetCatalogStr(catalog,1100,"stalled at")
global.str.BYTES=GetCatalogStr(catalog,1101,"bytes")
global.str.RXERR=GetCatalogStr(catalog,1102,"Error receiving")
global.str.SIZE=GetCatalogStr(catalog,1103,"Size")
global.str.UNKNOWSIZE=GetCatalogStr(catalog,1104,"unknown size")
global.str.AT=GetCatalogStr(catalog,1105,"at")
global.str.ASPECTED=GetCatalogStr(catalog,1106,"Aspcted")
global.str.RECEIVED=GetCatalogStr(catalog,1107,"received")
global.str.INTERRUPTED=GetCatalogStr(catalog,1108,"Transfer interrupted")
global.str.RESOLVINGHOST=GetCatalogStr(catalog,1109,"Resolving host")
global.str.HOSTNTFOUND=GetCatalogStr(catalog,1110,"Host not found")
global.str.NOSOCKET=GetCatalogStr(catalog,1111,"No socket")
global.str.CONNECTING=GetCatalogStr(catalog,1112,"Connecting...")
global.str.CANTCONNECT=GetCatalogStr(catalog,1113,"Can't connect to")
global.str.SENDINGREQ=GetCatalogStr(catalog,1114,"Sending request...")
global.str.TXERR=GetCatalogStr(catalog,1115,"Error sending")
global.str.HEADREC=GetCatalogStr(catalog,1116,"Receiving and parsing head...")
global.str.RXTIMEOUTERR=GetCatalogStr(catalog,1117,"Error receiving or timeout")
global.str.EMPTY=GetCatalogStr(catalog,1118,"The requested document contains no data")
global.str.BADANSWER=GetCatalogStr(catalog,1119,"Bad answer from server")
global.str.NOTMODIFIED=GetCatalogStr(catalog,1120,"File was not modified since")
global.str.SERVERERR=GetCatalogStr(catalog,1121,"Server error")
global.str.UNKONW=GetCatalogStr(catalog,1122,"unknown")
global.str.EXITED=GetCatalogStr(catalog,1123,"Transfer terminated")
call CloseCatalog(catalog);
return
/**************************************************************************/
/*$VER: GetItFun.rexx 4.5 (17.5.99)*/